//
//  ViewController.m
//  movie
//
//  Created by 白浩洋 on 2018/3/13.
//  Copyright © 2018年 白浩洋. All rights reserved.
//

#import "ViewController.h"
#import "imgDetailViewController.h"
#import "MBProgressHUD.h"
#import "NewPagedFlowView.h"
#import "PGIndexBannerSubiew.h"
#import "HotSearchCollectionViewCell.h"
#import "HotHeadFreeCollectionReusableView.h"
#import "MoviePlayViewController.h"
#import "collectionHeaderView.h"
#import "movieDetailViewController.h"
#import "SELUpdateAlert.h"
#import "MusicCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,MusicCellDelegate>
@property(nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) UILabel *indicateLabel;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation ViewController
{
    NSInteger index;
    BOOL isDown;
    NSMutableArray *DateArr;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [SVProgressHUD showInfoWithStatus:@"正在加载中。。。"];
    self.view.backgroundColor = [UIColor whiteColor];
    [self addCustomTitleWithTitle:@"影视"];
    index = 0;
    isDown = YES;
    [self requestDate];
    [self tableView];
    [self getState];
    [self getStar];
    DateArr = [[NSMutableArray alloc]init];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getStar)
                                                 name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void)getStar{
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"star"];
    //查找GameScore表里面id为0c6db13c的数据
    [bquery whereKey:@"userid" equalTo:[BmobUser currentUser].username];
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        for (BmobObject *obj in array) {
            NSString *sum = [obj objectForKey:@"star"];
            [NBATools setstar:[sum integerValue]];
            break;
        }
    }];
}
- (void)logOutClick{
    [BmobUser logout];
    NSUserDefaults *nsud = [NSUserDefaults standardUserDefaults];
    [nsud removeObjectForKey:@"username"];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"logOut" object:nil];
}
- (void)getState{
    //查找GameScore表
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"audit"];
    //查找GameScore表里面id为0c6db13c的数据
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error){
            //进行错误处理
        }else{
            //表里有id为0c6db13c的数据
            if (array.count >0) {
                //得到playerName和cheatMode
                NSDictionary * dic = array[0];
                BOOL cheatMode = [[dic objectForKey:@"isopen"]  isEqualToString:@"开放"];
                float versionss = [[dic objectForKey:@"version"] floatValue];
                NSString *aboutStr =[dic objectForKey:@"about"];
                [NBATools setAbout:aboutStr];
                [NBATools setSHState:cheatMode];
                [NBATools setVersions:versionss];
                if([NBATools getVersions] > Version){
                    [SELUpdateAlert showUpdateAlertWithVersion:[NSString stringWithFormat:@"%.2f",[NBATools getVersions]] Descriptions:@[@" ",@" "]];
                }
            }
        }
    }];
}

#pragma mark NewPagedFlowView Delegate
- (void)didSelectCell:(UIView *)subView withSubViewIndex:(NSInteger)subIndex {
    MoviePlayViewController *mv = [[MoviePlayViewController alloc]init];
    mv.hidesBottomBarWhenPushed = YES;
    mv.playType = 101;
    mv.MovieUrl =self.imageArray[subIndex][@"jumpUrl"];
    [self.navigationController pushViewController:mv animated:YES];
}
-(void)musicToGroupFromDic:(NSDictionary*)groupType{
    NSArray *arr = [NBATools gethistory];
    for (int i = 0; i<arr.count; i++) {
        NSDictionary *dic = arr[i];
        if([dic[@"videoUrl"] isEqualToString:groupType[@"videoUrl"]]){
            MoviePlayViewController *mv = [[MoviePlayViewController alloc]init];
            mv.hidesBottomBarWhenPushed = YES;
            mv.MovieName = groupType[@"des"];
            mv.MovieUrl =groupType[@"videoUrl"];
            mv.shoucangdic = groupType;
            [self.navigationController pushViewController:mv animated:YES];
            return;
        }
    }
    NSInteger i = [groupType[@"shoujia"] intValue];
    NSInteger totalStar = [NBATools getstar];
    if(totalStar - i > 0){
        [NBATools setstar:totalStar - i];
        [NBATools setstarOnline:(totalStar - i)];
        NSArray * hisArr = [NBATools gethistory];
        NSMutableArray *mutArr = [[NSMutableArray alloc]initWithArray:hisArr];
        [mutArr insertObject:groupType atIndex:0];
        [NBATools sethistory:mutArr];
        MoviePlayViewController *mv = [[MoviePlayViewController alloc]init];
        mv.hidesBottomBarWhenPushed = YES;
        mv.MovieName = groupType[@"des"];
        mv.MovieUrl =groupType[@"videoUrl"];
        mv.shoucangdic = groupType;
        [self.navigationController pushViewController:mv animated:YES];
    }else{
        UIAlertController *alert = [NBATools showAlert];
        [self presentViewController:alert animated:YES completion:nil];
    }
}
-(void)MoreMusicTo:(NSDictionary *)model atIndex:(NSInteger)index{
    
    movieDetailViewController *mvc = [[movieDetailViewController alloc]init];
    NSDictionary *dic =  self.dataSource[index];
    mvc.typeDic = dic;
    mvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:mvc animated:YES];
    
}
- (void)requestDate{
    BmobQuery *bquery = [BmobQuery queryWithClassName:@"kindTab"];
    bquery.limit = 12;
    bquery.skip = index * 12;
    [bquery orderByAscending:@"type"];
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        [SVProgressHUD dismiss];
        if(error){
        }else{
            if(array.count > 0 && isDown){
                [self.dataSource removeAllObjects];
            }
            for (BmobObject *obj in array) {
                NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
                [dic setObject:obj.objectId forKey:@"id"];
                [dic setObject:[obj objectForKey:@"kindname"] forKey:@"des"];
                [dic setObject:[obj objectForKey:@"type"] forKey:@"type"];
                [dic setObject:[obj objectForKey:@"desjson"] forKey:@"desjson"];
                [self.dataSource addObject:dic];
            }
            [self.tableView reloadData];
        }
    }];
}
-(NSMutableArray *)dataSource{
    if(!_dataSource){
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}
- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - iPhoneXTabHeigth - 64 - 44) style:0];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        [_tableView registerNib:[UINib nibWithNibName:@"MusicCell" bundle:nil] forCellReuseIdentifier:@"MusicCellID"];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MusicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MusicCellID"];
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    if(!cell)
    {
    cell = [[MusicCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MusicCellID"];
    }
    cell.delegate = self;
    cell.dataDic = [self.dataSource objectAtIndex:indexPath.row];
    [cell loadCell:indexPath];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return PXChange(460);
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
@end
